package com.motong.gongdan.util;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author:fff
 * @time:2022/6/7
 */
public class RedisChangeUtil {

//    public static RedisTemplate setDataBase(int num,Map<String,String> map,RedisTemplate redisTemplate) {
//        LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
//        if (connectionFactory != null && num != connectionFactory.getDatabase()) {
//            connectionFactory.setDatabase(num);
//            redisTemplate.setConnectionFactory(connectionFactory);
//            connectionFactory.resetConnection();
//            connectionFactory.afterPropertiesSet();
//        }
//        if(map!=null && !map.isEmpty() && !map.get("host").equals(connectionFactory.getHostName())){
//            changeRedis(redisTemplate);
//        }
////        handleRedisJackson(redisTemplate);
//        return redisTemplate;
//    }
//
//    public static RedisTemplate changeRedis(RedisTemplate redisTemplate) {
//        LettuceConnectionFactory redisConnectionFactory = (LettuceConnectionFactory) redisTemplate
//                .getConnectionFactory();
//        redisConnectionFactory.destroy();
//        LettuceConnectionFactory factory = getLettuceConnectionFactory();
//        factory.afterPropertiesSet();
//        redisTemplate.setConnectionFactory(factory);
////        handleRedisJackson(redisTemplate);
//        return redisTemplate;
//    }
//
//    public static LettuceConnectionFactory getLettuceConnectionFactory(){
//        Map<String,String> map = RedisUtil.getOther();
//        if(map!=null){
//            RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
//            redisStandaloneConfiguration.setDatabase(Integer.valueOf(map.get("db")));
//            redisStandaloneConfiguration.setHostName(map.get("host"));
//            redisStandaloneConfiguration.setPort(Integer.valueOf(map.get("port")));
//            redisStandaloneConfiguration.setPassword(RedisPassword.of(map.get("pwd")));
//            LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration,getLettuceClientConfiguration());
//            return factory;
//        }
//        return null;
//    }
//
//    private static LettuceClientConfiguration getLettuceClientConfiguration(){
//        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
//        poolConfig.setMaxIdle(100);
//        poolConfig.setMinIdle(0);
//        poolConfig.setMaxTotal(10000);
//        ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
//                .enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT)
//                .enableAllAdaptiveRefreshTriggers()
//                .adaptiveRefreshTriggersTimeout(Duration.ofSeconds(25)).enablePeriodicRefresh(Duration.ofSeconds(20)).build();
//        LettucePoolingClientConfiguration lpc = LettucePoolingClientConfiguration.builder()
//                .poolConfig(poolConfig)
//                .clientOptions(ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build()).build();
//        return lpc;
//    }

    private static void handleRedisJackson(RedisTemplate template){
        //JSON序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer =
                new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL);
        /*om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);*/
        jackson2JsonRedisSerializer.setObjectMapper(om);

        //String序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        //key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);

        //hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);

        //value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);

        //hash的value序列化采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
    }
}
